<?php

// 数据库配置
include 'config.php';

// 检查是否是 GET 请求
if ($_SERVER["REQUEST_METHOD"] == "GET") {
    // 获取 GET 数据
    $kami = $_GET['kami'] ?? '';

    // 检查卡密是否存在
    $checkStmt = $conn->prepare("SELECT * FROM kami WHERE kami = ?");
    $checkStmt->bind_param("s", $kami);
    $checkStmt->execute();
    $result = $checkStmt->get_result();
    $row = $result->fetch_assoc();
    if ($row) {
        // 如果卡密存在，根据类型编号返回信息
        $typeDescription = "未知类型";
        switch ($row['type']) {
            case 1:
                $typeDescription = "月卡";
                break;
            case 2:
                $typeDescription = "年卡";
                break;
            case 3:
                $typeDescription = "永久卡";
                break;
        }

        // 检查卡密状态
        $item = "未知状态";
        if ($row['zt'] == 1) {
            $item = "已使用";
        } elseif ($row['zt'] == 0) {
            $item = "未使用";
        }

        $response = [
            "code" => "200",
            "msg" => "查询成功",
            "type" => $typeDescription,
            "item" => $item
        ];
    } else {
        // 如果卡密不存在
        $response = [
            "code" => "404",
            "msg" => "卡密不存在"
        ];
    }

    // 返回 JSON 响应
    echo json_encode($response, JSON_UNESCAPED_UNICODE);
} else {
    // 如果不是 GET 请求，返回错误
    header("HTTP/1.1 405 Method Not Allowed");
    exit("仅支持 GET 请求");
}

// 关闭数据库连接
$conn->close();
?>